package com.ticket.system.service.impl;

import com.ticket.common.utils.DateUtils;
import com.ticket.system.domain.YanchuOrder;
import com.ticket.system.domain.YanchuScore;
import com.ticket.system.mapper.YanchuScoreMapper;
import com.ticket.system.mapper.YanchuSessionMapper;
import com.ticket.system.service.ITYanchuPointsRuleService;
import com.ticket.system.service.IYanchuOrderLogService;
import com.ticket.system.service.IYanchuScoreService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 用户积分Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-09
 */
@Service
public class YanchuScoreServiceImpl implements IYanchuScoreService
{
    private static final Logger logger = LoggerFactory.getLogger(YanchuScoreServiceImpl.class);
    @Autowired
    private YanchuScoreMapper yanchuScoreMapper;

    @Autowired
    private YanchuSessionMapper yanchuSessionMapper;

    @Autowired
    private ITYanchuPointsRuleService pointsRuleService;


    @Autowired
    private IYanchuOrderLogService yanchuOrderLogService;

    /**
     * 查询用户积分
     * 
     * @param id 用户积分主键
     * @return 用户积分
     */
    @Override
    public YanchuScore selectYanchuScoreById(Long id)
    {
        return yanchuScoreMapper.selectYanchuScoreById(id);
    }

    /**
     * 查询用户积分列表
     * 
     * @param yanchuScore 用户积分
     * @return 用户积分
     */
    @Override
    public List<YanchuScore> selectYanchuScoreList(YanchuScore yanchuScore)
    {
        return yanchuScoreMapper.selectYanchuScoreList(yanchuScore);
    }

    /**
     * 新增用户积分
     * 
     * @param yanchuScore 用户积分
     * @return 结果
     */
    @Override
    public int insertYanchuScore(YanchuScore yanchuScore)
    {
        yanchuScore.setCreateTime(DateUtils.getNowDate());
        return yanchuScoreMapper.insertYanchuScore(yanchuScore);
    }

    /**
     * 修改用户积分
     * 
     * @param yanchuScore 用户积分
     * @return 结果
     */
    @Override
    public int updateYanchuScore(YanchuScore yanchuScore)
    {
        yanchuScore.setUpdateTime(DateUtils.getNowDate());
        return yanchuScoreMapper.updateYanchuScore(yanchuScore);
    }

    /**
     * 批量删除用户积分
     * 
     * @param ids 需要删除的用户积分主键
     * @return 结果
     */
    @Override
    public int deleteYanchuScoreByIds(Long[] ids)
    {
        return yanchuScoreMapper.deleteYanchuScoreByIds(ids);
    }

    /**
     * 删除用户积分信息
     * 
     * @param id 用户积分主键
     * @return 结果
     */
    @Override
    public int deleteYanchuScoreById(Long id)
    {
        return yanchuScoreMapper.deleteYanchuScoreById(id);
    }

    @Override
    @Async
    public void updateNumsMoney(Integer count, BigDecimal price, Long id, Long wxUserId, Integer score, Long performId,
                                YanchuOrder yanchuOrder){
        logger.info("updateNumsMoney id:{} wxUserId:{} count:{} price:{} score:{}",id,wxUserId,count,price,score);
//        if(score == 0){
//            score=1;
//        }
        //更新票房 售出票数
        yanchuSessionMapper.updateNumsMoney(count,price,id);

        if(null != performId && yanchuOrder.getTicketType() == 1){
            yanchuOrderLogService.insertOrderLog(performId,count,yanchuOrder.getId(),yanchuOrder);
        }
        pointsRuleService.addPoints(2,price,wxUserId);
    }

    @Override
    @Async
    public void reduceNumsMoney(Integer count, BigDecimal price,Long id,Long wxUserId,Integer score){
        logger.info("reduceNumsMoney id:{} wxUserId:{} count:{} price:{} score:{}",id,wxUserId,count,price,score);
        //更新票房 售出票数
        yanchuSessionMapper.reduceNumsMoney(count,price,id);
        //更新积分
        yanchuScoreMapper.reduceScore(wxUserId,score);
    }

    public void resetScore(){
        yanchuScoreMapper.resetScore();
    }
}
